package rs.frenjoynet.core.ui.session;

import org.jboss.seam.annotations.*;
import org.jboss.seam.ScopeType;
import org.jboss.seam.log.Log;

import javax.faces.context.FacesContext;
import java.util.Stack;
import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User: ppopovic
 * Date: 30.10.2009.
 * Time: 10.57.40
 * To change this template use File | Settings | File Templates.
 */
@AutoCreate
@Name ("pageViewStack")
@Scope (ScopeType.CONVERSATION)
public class PageViewStack implements Serializable
{
	int count = 0;

	@Logger
	private Log log;

	public PageViewStack()
	{
		views = new Stack<String>();
	}

	public void push()
	{
		String a = FacesContext.getCurrentInstance().getViewRoot().getViewId();
		log.debug("Push view-id : " + a);
		if (a == null)
		{
			return;
		}
		if (views.empty() || !views.peek().equals(a))
		{
			views.push(a);
			count++;
		}
	}

	/*
		 * This pops the top item and then returns the next item This is because the
		 * top item is always the active page, and we will be trying to move the the
		 * active pages parent
		 */
	public String pop()
	{
		if (count > 1)
		{
			views.pop();
			count--;
		}
		return views.peek();
	}

	Stack<String> views;

	public int getCount()
	{
		return count;
	}

	public void setCount(int count)
	{
		this.count = count;
	}
}
